﻿using System.Text;
using System.Threading.Tasks;
using Demo.Api.Data;
using Demo.Api.Models;
using Demo.Api.Services;
using KmFlow;
using Microsoft.AspNetCore.Mvc;

namespace Demo.Api.Controllers
{
    /// <summary>
    /// 节点
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class NodeController : ControllerBase
    {
        private readonly NodeRepository _repository;

        public NodeController(NodeRepository repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 获取一个流程图的所有的节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Gets(long id)
        {
            var result = await _repository.GetsAsync(id);
            return Ok(result);
        }
        /// <summary>
        /// 获取一个节点的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(long id)
        {
            var result = await _repository.GetAsync(id);
            return Ok(result);
        }
        /// <summary>
        /// 添加一个节点
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add(NodeDto model)
        {
            var temp = model.To();
            temp.Id = IdWorker.NextId();
            temp.OrClear = false;
            temp.Logic = 1;
            if (await _repository.Add(temp))
                return Ok(temp.Id);
            else
            {
                return BadRequest("添加失败");
            }
        }
        /// <summary>
        /// 更新节点信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update(long id, NodeDto model)
        {
            if (id < 1)
                return BadRequest("Id不正确");
            var temp = await _repository.GetAsync(id);
            temp.Id = id;
            temp.Name = model.Name;
            //temp.FlowChartId = model.FlowChartId;
            temp.Logic = model.Logic;
            temp.NodeType = model.NodeType;
            temp.OrClear = model.OrClear;
            //temp.PositionX = model.PositionX;
            //temp.PositionY = model.PositionY;
            temp.RuleId = model.RuleId;
            temp.SeparateNodeId = model.SeparateNodeId;
            temp.Code = model.Code;
            temp.Remarks = model.Remarks;
            if (await _repository.Update())
                return Ok();
            else
            {
                return BadRequest("更新失败");
            }
        }
        /// <summary>
        /// 更新节点的位置
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdatePosition(long id, NodeDto model)
        {
            if (id < 1)
                return BadRequest("Id不正确");
            var temp = await _repository.GetAsync(id);
            temp.Id = id;
            temp.PositionX = model.PositionX;
            temp.PositionY = model.PositionY;
            if (await _repository.Update())
                return Ok();
            else
            {
                return BadRequest("更新失败");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Delete(long id)
        {
            var result = await _repository.Delete(id);
            return Ok(result);
        }
    }
}